package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.ExamPaperQuestionCustomerAnswer;
import com.mindskip.xzs.domain.other.ExamPaperAnswerUpdate;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.student.question.answer.QuestionPageStudentRequestVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface ExamPaperQuestionCustomerAnswerMapper extends MyMapper<ExamPaperQuestionCustomerAnswer> {
//    int deleteByPrimaryKey(Integer id);
//
//    int insert(ExamPaperQuestionCustomerAnswer record);
//
//    int insertSelective(ExamPaperQuestionCustomerAnswer record);
//
//    ExamPaperQuestionCustomerAnswer selectByPrimaryKey(Integer id);
//
//    int updateByPrimaryKeySelective(ExamPaperQuestionCustomerAnswer record);
//
//    int updateByPrimaryKey(ExamPaperQuestionCustomerAnswer record);

    String queryParams=" id, question_id questionId, exam_paper_id examPaperId, exam_paper_answer_id examPaperAnswerId, " +
            "question_type questionType, subject_id subjectId, customer_score customerScore, question_score questionScore," +
            " question_text_content_id questionTextContentId, answer, text_content_id textContentId, do_right doRight, " +
            "create_user createUser, create_time createTime, item_order itemOrder ";


    @Select("select\n" +queryParams+
            "    from t_exam_paper_question_customer_answer\n" +
            "    where exam_paper_answer_id = #{id}\n" +
            "    order by item_order")
    List<ExamPaperQuestionCustomerAnswer> selectListByPaperAnswerId(@Param("id") Integer id);

    @Select("select " +queryParams+
            "FROM t_exam_paper_question_customer_answer " +
            "where do_right=FALSE \n" +
            "and create_user=#{createUser}")
    List<ExamPaperQuestionCustomerAnswer> studentPage(QuestionPageStudentRequestVM requestVM);

//    int insertList(List<ExamPaperQuestionCustomerAnswer> list);

    @Select("SELECT count(*) from t_exam_paper_question_customer_answer")
    Integer selectAllCount();

    @Select("SELECT create_time as name,COUNT(create_time) as value from\n" +
            "\t\t        (\n" +
            "\t\t\t\t  SELECT DATE_FORMAT(create_time,'%Y-%m-%d') as create_time from t_exam_paper_question_customer_answer\n" +
            "\t\t\t\t\tWHERE  create_time  between  #{startTime}  and  #{endTime}\n" +
            "\t\t\t\t) a\n" +
            "\t\tGROUP BY create_time")
    List<KeyValue> selectCountByDate(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Update("<script>" +
            "<foreach collection=\"list\" item=\"item\" >\n" +
            "        update t_exam_paper_question_customer_answer\n" +
            "        set customer_score=#{item.customerScore} , do_right=#{item.doRight}\n" +
            "        where id=#{item.id}\n" +
            "    </foreach>" +
            "</script>")
    int updateScore(@Param("list") List<ExamPaperAnswerUpdate> examPaperAnswerUpdates);
}
